File Input
Let's calculate the BMI's of our favorite team's players.
A file named player_stats.txt
contains a matrix with the information about each player. Save a copy of this file
to your current directory.
(You can do this by right-clicking in the link above and selecting "Save Target As...".)
Open a new m-file to create a new program script.
Start out by clearing the variables with the clear
command.
Then comment on the purpose of the program using the percent sign.
We will be displaying the player information in user friendly format,
so you may want to include an example of the output in the comments to
help you remember that as well.
Next, we will load (read) the data from the file and store it in a variable in the program. There are a few ways to read data from a file into your program:
- If the data will always be in a file with the same name and you
know the name of that file. Use the
load
command directly with the file name to load the data into a variable with the same name as the file:load player_stats.txt
This saves the numbers from the file into a matrix variable named
player_stats
. The first column contains the players' numbers. The second and third are the height in feet and inches, respectively. The fourth is the weight in pounds. - If the data will always be in a file with the same name and you
know the name of that file, but you wish to load the data into a variable
with a different name, assign the results of the
load
command with either syntax shown here:stats = load( 'player_stats.txt' );
or
filename = 'player_stats.txt'; stats = load( filename );
Both of the above versions do the same thing, but the second version is a little more flexible and is the better choice if you need to use the filename in any other place in your program.
- A third way, is to ask the user for the file name and then load the data
from that file into a variable. This code fragment uses the
input
command to get the name of the file and then uses theload
command to read the data from the file into the program:stats_file_name = input( 'What is the name of the player stats file: ', 's' ); stats = load( stats_file_name );
Notice, the use of a second argument to the
input
command. The's'
tells Matlab to interpret the user's input as a string of characters and not as a Matlab expression.
After loading the data into a single matrix, it is often more convenient to separate each column into its own vector, using appropriate variable names. For example, you might use the following to put the numbers of all the players into one vector:
num = player_stats(:,1);
Once you have the heights and weights in vectors, convert them into metric units, using variable names m and kg, respectively. Then calculate the BMI of each player:
BMI = kg ./ m .^2
If you have the BMI function we defined in an earlier lesson, you can also use that to compare the BMI of each player. Be sure to send it metric or standard units as appropriate for how the function is defined.
Test your program code that calculates the BMI's and use Matlab's debugger to help you identify the cause of any errors that might occur.